package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.dao.ReturnRegisterDao;
import com.ytjj.qmyx.admin.model.RegisterInsertRequest;
import com.ytjj.qmyx.admin.model.request.ReturnRegisterRequest;
import com.ytjj.qmyx.admin.service.ReturnRegisterService;
import com.ytjj.qmyx.mall.mapper.OrdersInfoMapper;
import com.ytjj.qmyx.mall.model.OrdersInfo;
import com.ytjj.qmyx.mall.model.ReturnRegister;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @Version: 1.0
 *
 * 原路登记页
 */
@Slf4j
@RestController
@RequestMapping("/returnRegister")
public class ReturnRegisterController {

    @Resource
    private ReturnRegisterDao returnRegisterDao;
    @Resource
    private OrdersInfoMapper ordersInfoMapper;
    @Autowired
    private ReturnRegisterService returnRegisterService;

    /**
     * 检查是否已存在原路返回
     * @param request
     * @return true-不存在 false-已存在
     */
    @PostMapping("/check")
    public CommonResult checkCanInsertReturnRegister(@RequestBody RegisterInsertRequest request) {
        Integer num = returnRegisterService.selectByOrderInfoId(request.getOrderInfoId());
        if (null == num || num == 0) {
            return CommonResult.success(true);
        }
        return CommonResult.success(false);
    }

    /**
     * 增加原路登记
     * @param request
     * @return
     */
    @PostMapping("/insert")
    @OperLog(operModule = "原路登记页模块", operDesc = "新增", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertReturnRegister(@RequestBody RegisterInsertRequest request) {
        Integer count = returnRegisterService.insertReturnRegister(request);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 原路登记列表
     * @param returnRegisterRequest
     * @return
     */
    @PostMapping("/list")
    @OperLog(operModule = "原路登记页模块", operDesc = "列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectList(@RequestBody ReturnRegisterRequest returnRegisterRequest) {
        return CommonResult.success(returnRegisterService.selectList(returnRegisterRequest));
    }

    /**
     * 编辑原路登记
     * @param returnRegisterRequest
     * @return
     */
    @PostMapping("/update")
    @OperLog(operModule = "原路登记页模块", operDesc = "编辑", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateReturnRegister(@RequestBody ReturnRegisterRequest returnRegisterRequest) {
        return CommonResult.success(returnRegisterService.updateReturnRegister(returnRegisterRequest));
    }

    @GetMapping("/delete")
    @OperLog(operModule = "原路登记页模块", operDesc = "移除", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult delReturnRegister(@RequestParam Integer id) {
        Integer count = returnRegisterService.delReturnRegister(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/refundOpear")
    @OperLog(operModule = "原路登记页模块", operDesc = "退款", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult refundOpear(@RequestParam Integer id, @RequestParam String refundDesc) {
        try {
            // 1.获取原路返回数据信息
            ReturnRegister register = returnRegisterDao.getReturnRegisterById(id);
            OrdersInfo ordersInfo = ordersInfoMapper.selectByPrimaryKey(register.getOrderInfoId());
            returnRegisterService.refund(id, refundDesc,register,ordersInfo);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

    @GetMapping("/refundOperaRpc")
    public String refundOperaRpc(@RequestParam Integer id, @RequestParam String refundDesc) {
        // 1.获取原路返回数据信息
        ReturnRegister register = returnRegisterDao.getReturnRegisterById(id);
        OrdersInfo ordersInfo = ordersInfoMapper.selectByPrimaryKey(register.getOrderInfoId());
        returnRegisterService.refund(id, refundDesc,register,ordersInfo);
        return "200";
    }

    @PostMapping("/batchRefundOpear")
    @OperLog(operModule = "原路登记页模块", operDesc = "批量退款", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult batchRefundOpear(@RequestBody ReturnRegisterRequest returnRegisterRequest) {
        List<Map<String, String>> mapList = new ArrayList<>();
        returnRegisterRequest.getIds().forEach(id -> {
            // 1.获取原路返回数据信息
            ReturnRegister register = returnRegisterDao.getReturnRegisterById(id);
            OrdersInfo ordersInfo = ordersInfoMapper.selectByPrimaryKey(register.getOrderInfoId());
            Map<String, String> map = new HashMap<>();
            try {
                returnRegisterService.refund(id, returnRegisterRequest.getRefundDesc(),register,ordersInfo);
                map.put("childOrderNo", ordersInfo.getChildOrderNo());
                map.put("remark", "提交成功,稍后刷新查看最新退款状态");
            } catch (Exception e) {
                log.info("{}",e);
                map.put("childOrderNo", ordersInfo.getChildOrderNo());
                map.put("remark", e.getMessage());
            }
            mapList.add(map);
        });
        return CommonResult.success(mapList);
    }
}
